﻿/******************************************************************************************************
Copyright (c) 2014 Steven Giacomelli (stevegiacomelli@gmail.com)

Permission is hereby granted, free of charge, to any person obtaining a copy of this
software and associated documentation files (the "Software"), to deal in the Software
without restriction, including without limitation the rights to use, copy, modify,
merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies
or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
******************************************************************************************************/

using System;
using System.Collections.Generic;

namespace SharpGeo
{
    /// <summary>
    /// Represents a grouping of map layers that are rendered together as a map
    /// </summary>
    public class Map : IObservable<MapLayer>
    {
        #region Constructor

        /// <summary>
        /// Constructs a map
        /// </summary>
        public Map()
        {
            BackgroundColor = StandardColors.Aquamarine;
        }

        #endregion Constructor

        #region Fields

        private List<IObserver<MapLayer>> _observers = new List<IObserver<MapLayer>>();
        private List<MapLayer> _layers = new List<MapLayer>();

        #endregion Fields

        #region Properties

        /// <summary>
        /// Gets the maplayer at the specified index
        /// </summary>
        /// <param name="index">The index to retrieve the map layer at</param>
        /// <returns>The map layer</returns>
        public MapLayer this[int index] { get { return _layers[index]; } }

        /// <summary>
        /// Gets the number of maplayers associated with this map
        /// </summary>
        public int LayerCount { get { return _layers.Count; } }

        /// <summary>
        /// The map's background color
        /// </summary>
        public Color BackgroundColor { get; set; }

        #endregion Properties

        #region ObserverPattern Implementations

        /// <summary>
        /// Called by the changing map layer
        /// </summary>
        /// <param name="layer"></param>
        public void OnMapLayerChanged(MapLayer layer)
        {
            lock (_observers)
            {
                for (int i = 0; i < _observers.Count; i++)
                {
                    _observers[i].OnNext(layer);
                }
            }
        }

        /// <summary>
        /// Subscribe to recieve notifications when a map layer associated with the map changes
        /// </summary>
        /// <param name="observer">The observer to subscribe</param>
        /// <returns>An unsubscription object</returns>
        public IDisposable Subscribe(IObserver<MapLayer> observer)
        {
            lock (_observers)
            {
                if (!_observers.Contains(observer))
                    _observers.Add(observer);

                return new Unsubscriber(_observers, observer);
            }
        }

        #region Unsubscriber class

        private class Unsubscriber : IDisposable
        {
            private List<IObserver<MapLayer>> _observers;
            private IObserver<MapLayer> _observer;

            public Unsubscriber(List<IObserver<MapLayer>> observers, IObserver<MapLayer> observer)
            {
                this._observers = observers;
                this._observer = observer;
            }

            public void Dispose()
            {
                lock (_observers)
                {
                    if (!(_observer == null)) _observers.Remove(_observer);
                }
            }
        }

        #endregion Unsubscriber class

        #endregion ObserverPattern Implementations
    }
}